package ee.az.test.math;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Artur Zinatullin (artur.zinatullin@gmail.com) 2010
 */
public class CharStatistics {

	private static void printHistogram(Map<Integer, Integer> histogram) {
		List<Integer> keys = new ArrayList<Integer>(histogram.keySet());
		Collections.sort(keys);
		for (Integer i : keys) {
			System.out.println("" + ((char)(int)i) + ", " + (int)histogram.get(i));
		}
	}
	
	public static void main(String[] args) {
		InputStreamReader isr = null;
		try {
			isr = new InputStreamReader(new FileInputStream(args[0]));
			int c;
			Map<Integer, Integer> histogram = new HashMap<Integer, Integer>();
			while ((c = isr.read()) != -1) {
				Integer value = histogram.get(c);
				if (value == null) {
					histogram.put(c, 1);
				} else {
					histogram.put(c, value+1);
				}
			}
			printHistogram(histogram);
		} catch (IOException e) {
			e.printStackTrace(System.err);
		} finally {
			try {
				isr.close();
			} catch (Exception e) {
				e.printStackTrace(System.err);
			}
		}
	}
}
